package org.example.common;


import lombok.Data;
import org.example.constant.CodeEnum;

import java.io.Serializable;

/**
 * 后端统一返回结果
 * @param <T>
 */
@Data
public class Result<T> implements Serializable {

    private Integer status; //状态成功success，失败error
    private String message; //错误信息
    private T data; //数据

    public static <T> Result<T> success() {
        Result<T> result = new Result<T>();
        result.status = 200;
        return result;
    }

    public static <T> Result<T> success(T object) {
        Result<T> result = new Result<T>();
        result.data = object;
        result.status = 200;
        return result;
    }

    public static <T> Result<T> success(T object, String message) {
        Result<T> result = new Result<T>();
        result.message = message;
        result.data = object;
        result.status = 200;
        return result;
    }

    public static <T> Result<T> error(String msg) {
        Result result = new Result();
        result.message = msg;
        result.status = 500;
        return result;
    }

    public static <T> Result<T> error(CodeEnum codeEnum) {
        Result result = new Result();
        result.message = codeEnum.getMessage();
        result.status = codeEnum.getCode();
        return result;
    }

    public static <T> Result<T> error(Integer code, String message) {
        Result result = new Result();
        result.message = message;
        result.status = code;
        return result;
    }

    public static <T> Result<T> success(String message)
    {
        Result<T> result = new Result<T>();
        result.message = message;
        result.status = 200;
        return result;
    }

}
